import { defineStore } from "pinia";
import { Card } from "@/types/card";
import { Query } from "@/types/query";
import { reactive } from "vue";
import { ordObj, uuid } from "@/utils/util";

const DOTOLIST = "dotoList"

export const useDotoStore = defineStore('doto', {
  state: () => ({
    dotoList: reactive<Array<Card>>(JSON.parse(localStorage.getItem(DOTOLIST) || '[]')),
    groupList: reactive<Array<String>>([])
  }),
  getters: {
    length: (state) => state.dotoList.length
  },
  actions: {
    /**
     * 初始化dotoList
     */
    initDotoList() {
      this.dotoList = reactive<Array<Card>>(JSON.parse(localStorage.getItem(DOTOLIST) || '[]'))
    },

    /**
     * 添加代办事项
     * @param card 代办事项
     */
    addDoto(card: Card): void {
      card.id = uuid()
      card.isFinish = card.isRelease = card.isFlag = false
      // 向头部添加元素
      this.dotoList.unshift(card)
      this.writeDotoToLocalStorage()
    },

    /**
     * 移除代办事项
     * @param id 代办事项id
     */
    removeDoto(id: String): void {
      this.dotoList.map((item, index) => {
        if (item.id === id) {
          this.dotoList.splice(index, 1)
        }
      })
      this.writeDotoToLocalStorage()
    },

    /**
     * 获取所有组别
     */
    initGroupList(): void {
      this.groupList = reactive<Array<String>>([])
      this.dotoList.map(doto => {
        if (doto.group && !this.groupList.includes(doto.group))
          this.groupList.push(doto.group)
      })
    },

    /**
     * 写dotoList到localStorage
     * 初始化groupList
     */
    writeDotoToLocalStorage() {
      this.initGroupList()
      localStorage.setItem(DOTOLIST, JSON.stringify(this.dotoList))
    },

    /**
     * 条件查询代办事项
     * @param params 查询条件
     * @returns 符合查询条件的待办事项
     */
    queryByCondition(...params: Array<Query>): Array<Card> {
      this.initDotoList()
      const dotoList = reactive<Array<Card>>(ordObj(this.dotoList))
      return dotoList.filter(doto => {
        let flag = true
        params.map(param => {
          flag = param.logic(doto) && flag
        })
        return flag
      })
    },

    /**
     * 查找指定doto事项
     * @param id 事项id
     * @returns 事项
     */
    findDoto(id: String): Card | null {
      const dotos = this.dotoList.filter(doto => doto.id === id)
      if (dotos.length !== 1)
        return null
      return dotos[0]
    },

    /**
     * 修改代办事项
     * @param doto 
     * @returns 
     */
    updateDoto(doto: Card): String | void {
      const oldDoto = this.findDoto(doto.id ? doto.id : '')
      if (!oldDoto) return '代办事项【' + doto.id + '】不存在'
      const index = this.dotoList.indexOf(oldDoto)
      this.dotoList.splice(index, 1, doto)
      this.writeDotoToLocalStorage()
    },

    /**
     * 删除代办事项
     * @param id 事项id
     * @returns 
     */
    deleteDoto(id: String | undefined) {
      if (!id) return
      const doto = this.findDoto(id)
      if (!doto) return '代办事项【' + id + '】不存在'
      this.dotoList.splice(this.dotoList.indexOf(doto), 1)
      this.writeDotoToLocalStorage()
    }
  }
})